package org.e_gold.service.impl;

import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;
import org.e_gold.dao.SettledContractDAO;
import org.e_gold.model.Contract;
import org.e_gold.model.SettledContract;
import org.e_gold.service.AIService;
import org.e_gold.service.BrowserService;
import org.e_gold.service.UserAccountService;
import org.e_gold.util.CalcUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AIServiceImpl implements AIService {

    private static final Logger logger = Logger.getLogger(AIServiceImpl.class);
    @Autowired
    private UserAccountService userAccountService;

    @Autowired
    private SettledContractDAO settledContractDAO;

    @Autowired
    private BrowserService browserService;

    @Override
    @Transactional
    public Contract getBestContract(List<Contract> qualifyContracts) {

        if (qualifyContracts != null && qualifyContracts.size() > 0) {
            Collections.sort(qualifyContracts);
            for (Contract c : qualifyContracts) {
                if (c.getContractType().indexOf("Under") > -1) { // under
                    Float rate = c.getRate();
                    if (rate <= 1.1f && rate >= 1.04f) {
                        logger.info(" Contract qualify :" + c.getEvent().getName());
                        return c;
                    }
                }
            }
        }
        return null;
    }

    @Override
    public void virtualPlaceBet(Contract bestContract) {

        try {
            logger.info("::::::::::::::::::::::::::::::::::::::::::::");
            logger.info("::::::::::::::::::::::::::::::::::::::::::::");
            logger.info("@@ Virtual place bet for best event : " + bestContract.getEvent().getName());
            logger.info("@@ best contract : " + bestContract.getContractType());
            logger.info("@@ best rate : " + bestContract.getRate());
            logger.info("@@ current play time : " + bestContract.getEvent().getPlayTime());
            logger.info("@@ current goal : " + bestContract.getEvent().getTotalGoal());
            logger.info("::::::::::::::::::::::::::::::::::::::::::::");
            logger.info("::::::::::::::::::::::::::::::::::::::::::::");
            Float balance = userAccountService.getRealBalance();
            logger.info("virtualPlaceBet : " + balance);
            if (balance != 0f) {
                SettledContract settledContract = new SettledContract();
                settledContract.setContract(bestContract);
                settledContract.setBetAmount(balance);
                settledContract.setWonAmount(balance * bestContract.getRate());
                settledContract.setResultAmount(0F);
                settledContract.setRunning(true);
                settledContract.setUnderGoal(CalcUtil.getUnderTotalGoal(bestContract.getContractType()));
                settledContractDAO.insert(settledContract);
                browserService.placeBet();
            }
        } catch (Exception e) {
            logger.error(" virtualPlaceBet error");
            logger.error(e);
        }
    }

    @Override
    public Boolean calcResult(SettledContract running) {

        running.setWon(running.getCurrentGoal() < running.getUnderGoal());
        settledContractDAO.update(running);
        return running.getWon();
    }
}
